package com.cibfintech.rpa.jdwzcx.common.manager;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.boot.json.JsonParser;
import org.springframework.stereotype.Component;

import com.cibfintech.rpa.jdwzcx.common.properties.ChaojiyingProperties;

@Component
public class CaptchaManager {

    @Autowired
    private ChaojiyingProperties chaojiyingProperties;

    public String recognize(byte[] byteArr) {
        String picStr = recognize(byteArr, 4);
        return picStr;
    }

    public String recognize(byte[] byteArr, int codeLength) {
        String codetype = "1902";
        if (codeLength > 4 && codeLength <= 10)
            codetype = String.format("10%02d", codeLength);
        else if (codeLength <= 12)
            codetype = "1012";
        else
            codetype = "1020";

        String result = ChaoJiYing.postPic(chaojiyingProperties.getUsername(), chaojiyingProperties.getPassword(), chaojiyingProperties.getSoftid(), codetype,
                String.valueOf(codeLength), byteArr);

        JsonParser jsonParser = new GsonJsonParser();
        Map<String, Object> map = jsonParser.parseMap(result);
        String picStr = map.get("pic_str").toString();
        return picStr;
    }

}
